#include"MyPlayer.h"
typedef struct _LINKINFO{
DATAINFO*head;
DATAINFO*end;
int count;
uint32_t gettimestamp;
}LINKINFO;
static LINKINFO linkvideoinfo={NULL,NULL,0,0};
static LINKINFO linkaudioinfo={NULL,NULL,0,0};
static SDL_mutex*mut=NULL;
void InitSDLMutx(){
    mut=SDL_CreateMutex();
}
void UnInitSDLMutx(){
    SDL_DestroyMutex(mut);
    mut=NULL;
}

int IsVideoFastThenAudio(){
    int bFast=0;
    SDL_mutexP(mut);
    if(linkaudioinfo.gettimestamp>0){
        if(linkvideoinfo.gettimestamp>linkaudioinfo.gettimestamp){
           bFast=1;
        }else
        if((linkvideoinfo.gettimestamp+100)<linkaudioinfo.gettimestamp){
           bFast=-1;
        }    
    }
    SDL_mutexV(mut);
    return bFast;
}
static int  GetPacketNum(LINKINFO*info){
    int length=0;
    SDL_mutexP(mut);
    length=info->count;
    SDL_mutexV(mut);
    return length;
}
static void PushPacketToList(DATAINFO*data,LINKINFO*info){
    SDL_mutexP(mut);
    if(info->head==NULL){
        info->head=info->end=data;
    }else{
       info->end->Next=data;
       info->end=info->end->Next;
    }
    info->count++;
    SDL_mutexV(mut);
}
static DATAINFO*GetPacketFromList(LINKINFO*info){
    DATAINFO*pos=NULL;
    SDL_mutexP(mut);
    if(info->head!=NULL){
        pos= info->head;
        if(info->head==info->end){
            info->head=info->end=NULL;
        }else{
            info->head=info->head->Next;
        }
        info->gettimestamp=pos->timestamp;
        info->count--;
    }
    SDL_mutexV(mut);
    return pos;
}
int  GetVideoPacketNum(){
    return GetPacketNum(&linkvideoinfo);
}
DATAINFO*GetVideoPacket(){
    return GetPacketFromList(&linkvideoinfo);
}
void PushVideoPacket(uint8_t *buffer,int width,int height,uint32_t timestamp){
    DATAINFO*data=(DATAINFO*)malloc(sizeof(DATAINFO));
    memset(data,0,sizeof(DATAINFO));
    data->data=buffer;
    data->width=width;
    data->height=height;
	data->timestamp=timestamp;
    data->Next=NULL;
    PushPacketToList(data,&linkvideoinfo);
}
int  GetAudioPacketNum(){
    return GetPacketNum(&linkaudioinfo);
}
void PushAudioPacket(uint8_t *buffer,int length,uint32_t timestamp){
    DATAINFO*data=(DATAINFO*)malloc(sizeof(DATAINFO));
    memset(data,0,sizeof(DATAINFO));
    data->data=buffer;
    data->length=length;
	data->timestamp=timestamp;
    data->Next=NULL;
    PushPacketToList(data,&linkaudioinfo);
}

DATAINFO*GetAudioPacket(){
    return GetPacketFromList(&linkaudioinfo);
}
